'use strict';

const BaseController = require('./base');

class AppointmentController extends BaseController {
  get _service() {
    return 'appointment';
  }

  async create() {
    const { ctx } = this;
    const { startTime, endTime, phoneNum, persons, comment, houseId } = ctx.params();
    // 查询是否有与该时间段冲突的租约
    const order = await ctx.service.order.findRange({ startTime, endTime, status: [ 1, 2 ], houseId });
    // 如果有 则阻止此操作
    if (order) {
      this.error('该时间段已被他人使用');
      return;
    }
    const result = await this._getService().create({ startTime, endTime, phoneNum, persons, comment, status: 1, houseId, uid: ctx.userId });
    if (result) {
      this.success();
    } else {
      this.error('创建预约失败');
    }
  }

  async update() {
    const { ctx } = this;
    const { status, reason } = ctx.params();
    const { id } = ctx.params;
    const result = await this._getService().update({ status, reason, id });
    if (result) {
      this.success();
    } else {
      this.error('更新预约失败');
    }
  }

  async agree() {
    const { ctx } = this;
    const { id } = ctx.params;
    // 获取预约的信息
    const appointment = await this._getService().findOne({ id, simple: true });
    const { startTime, endTime, phoneNum, persons, uid, houseId } = appointment;
    // 查询是否有与该时间段冲突的租约
    const order = await ctx.service.order.findRange({ startTime, endTime, status: [ 1, 2 ], houseId });
    // 如果有 则阻止此操作
    if (order) {
      this.error('该时间段已被他人使用');
      return;
    }
    // 如果没有 继续该操作
    const result = await this._getService().update({ status: 3, id });
    if (result) {
      const house = await this.service.house.findOne({ id: houseId });
      const day = Math.ceil((new Date(endTime).getTime() - new Date(startTime).getTime()) / (1000 * 60 * 60 * 24));
      await this.service.order.create({ startTime, endTime, phoneNum, persons, amount: day * house.rent, uid, houseId, status: 1 });
      this.success();
    } else {
      this.error('更新预约失败');
    }
  }

  async disagree() {
    const { ctx } = this;
    const { reason } = ctx.params();
    const { id } = ctx.params;
    const result = await this._getService().update({ status: 2, reason, id });
    if (result) {
      this.success();
    } else {
      this.error('更新预约失败');
    }
  }

  async destroy() {
    await this._destory('删除预约失败');
  }

  async show() {
    const { ctx } = this;
    const { id } = ctx.params;
    const result = await this._getService().findOne({ id: +id });
    let persons = '';
    if (result.persons) {
      persons = await this.service.person.findAll({ ids: result.persons });
    }
    if (result) {
      this.success({ ...result, persons });
    } else {
      this.error('获取预约失败');
    }
  }

  // 获取所有预约
  async index() {
    const { ctx } = this;
    const { status, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ status, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取预约失败');
    }
  }

  // 获取登录人的发起的预约
  async indexMy() {
    const { ctx } = this;
    const { status, houseId, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ status, uid: ctx.userId, houseId, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取预约失败');
    }
  }

  // 获取登录人收到的租约
  async indexMyReview() {
    const { ctx } = this;
    const { status, houseId, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ status, houseId, oid: ctx.userId, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取预约失败');
    }
  }

  async statisticalAppointment() {
    const { ctx } = this;
    const result = await this._getService().statisticalAppointment({ uid: ctx.userId });
    if (result) {
      this.success(result.map(item => ({ ...item, value: +item['COUNT(*)'] })));
    } else {
      this.error('获取民宿失败');
    }
  }

  async statisticalAppointmentAll() {
    const result = await this._getService().statisticalAppointmentAll();
    if (result) {
      this.success(result.map(item => ({ ...item, value: +item['COUNT(*)'] })));
    } else {
      this.error('获取民宿失败');
    }
  }

  async statisticalAppointmentAdd() {
    const { ctx } = this;
    const { startTime, areaId } = ctx.params();
    const result = await this._getService().statisticalAppointmentAdd({ startTime, areaId });
    if (result && result[0]) {
      this.success(Object.entries(result[0]).map(item => ({ key: item[0] + '月', value: +item[1] })));
    } else {
      this.error('获取民宿失败');
    }
  }
}

module.exports = AppointmentController;
